clc;
clear;
% 读文件
ACCE_2x_1 = "D:\00_StudyRecord_EN\0_2024_summer_learning\Experiment\level1\20240702_JT_0_0_1.txt";
Read_file2 = "D:\00_StudyRecord_EN\0_2024_summer_learning\Experiment\level1\20003.ini";

% 放到一个矩阵中，循环
acce = {ACCE_2x_1};
save acce.mat;
% 对原始数据进行偏差校正和缩放，以便于后续的分析或处理
for i = 1:length(acce)
data = load(acce{i});
Ag(1:size(data,1),1:3,i) = data(:,1:3);
FS = 200; % 采样频率
pi = 3.1415926;
ts = 1/FS;
Params = load(Read_file2);
Ka0 = Params(11:13,:);
Ba0 = Params(14,:);
Ka2 = Params(15,:) * 1e-6;
Ka = zeros(3,3);
Ka(1,1) = 1/(FS * Ka0(1,1));
Ka(2,2) = 1/(FS * Ka0(2,2));
Ka(3,3) = 1/(FS * Ka0(3,3));
Ka(1,2) = Ka0(1,2)/60*pi/180 * Ka(2,2);
Ka(1,3) = Ka0(1,3)/60*pi/180 * Ka(3,3);
Ka(2,1) = Ka0(2,1)/60*pi/180 * Ka(1,1);
Ka(2,3) = Ka0(2,3)/60*pi/180 * Ka(3,3);
Ka(3,1) = Ka0(3,1)/60*pi/180 * Ka(1,1);
Ka(3,2) = Ka0(3,2)/60*pi/180 * Ka(2,2);
db = Ba0 * 0.01
A = Ag(1:size(data,1),1:3,i) * Ka';
AG = [(A(:,1)-db(1)*ts)*200, (A(:,2)-db(2)*ts)*200, (A(:,3)-db(3)*ts)*200];
end
% Allan 方差计算
Fs = 200; % 采样频率
t = 10; % 
tau = 1/Fs; % 时间间隔
N = size(AG, 1); % 数据长度
% 选择多个不同的 tau 值
tau_values = logspace(log10(1/Fs), log10(N/Fs/2), 50);
allan_var = zeros(length(tau_values), 3);
% 计算每个轴的 Allan 方差
for k = 1:3 % 对每个轴计算
accData = AG(:, k); % 选择当前轴的数据
for n = 1:length(tau_values)
tau = tau_values(n);
m = round(tau * Fs); % 对应于 tau 的采样点数
if m < 1 || m > N
continue;
end
% 计算不同时间间隔的平均值
interval = floor(N / m);
avg_values = zeros(interval, 1);
for j = 1:interval
segment = accData((j-1)*m + 1 : j*m);
avg_values(j) = mean(segment);
end
% 用于计算矩阵中元素的差分
diffs = diff(avg_values);
allan_var(n, k) = 0.5 * mean(diffs.^2);
end
end
% 绘制 Allan 方差曲线
figure;
loglog(tau_values, sqrt(allan_var(:,1)), 'r', 'DisplayName', 'X-axis');
hold on;
loglog(tau_values, sqrt(allan_var(:,2)), 'g', 'DisplayName', 'Y-axis');
loglog(tau_values, sqrt(allan_var(:,3)), 'b', 'DisplayName', 'Z-axis');
grid on;
title('Allan Variance Analysis');
xlabel('Time Interval \tau (s)');
ylabel('Allan Deviation \sigma(\tau)');
legend('show');
hold off;
% 显示结果
for k = 1:3
for n = 1:length(tau_values)
fprintf('轴 %d 时间间隔 %f 秒 对应的 Allan 方差为 %f\n', k, tau_values(n), allan_var(n, k));
end
end